import MyMath from "./math.js";
runOnStartup(async runtime => {
    runtime.addEventListener("tick", () => tick())
    runtime.getLayout("MainLevel").addEventListener("afterlayoutstart", () => init())
});
function tick() {
}
function init() {
    duckSpwn.init()
    duckSpwn.spwn()
}
export default class duckSpwn {
    static init() {
        runtime.objects.DuckObject.addEventListener("instancecreate", e => duckSpwn.OnDuckInstanceCreate(e, runtime));
        duckSpwn.changeLevel()
        duckSpwn.levelState = MyMath.randomNum(0, 4)
        duckSpwn.changeLevelTime = MyMath.randomNum(1000, 4000)
        duckSpwn.changeSpeedTime = MyMath.randomNum(3000, 8000)
    }
    static OnDuckInstanceCreate(e,runtime) {
        e.instance.instVars.state="go"
    }
    static changeLevel() {
        setInterval(function () {
            duckSpwn.levelState = MyMath.randomNum(1, 4)
        }, duckSpwn.changeLevelTime)
    }
    static spwn() {
        setInterval(function () {
            duckSpwn.changeSpeedTime = MyMath.randomNum(3000, 8000)
            /**@type {IObjectClass} */
            let DuckClass = runtime.objects.DuckObject
            // console.log(runtime.objects.duckPoint.instances())
            for (let duckSpwnPoint of runtime.objects.duckPoint.instances()) {
                if (duckSpwn.levelState == 1) {
                    if (duckSpwnPoint.instVars.id == 1) {
                        /**@type  {ISpriteInstance}  */
                        this.newDuckObject = DuckClass.createInstance("duckObjec", duckSpwnPoint.x, duckSpwnPoint.y, true)
                        // 調整duck方向
                        newDuckObject.instVars.id = duckSpwnPoint.instVars.id
                        if (duckSpwnPoint.instVars.dir == 1) {
                            this.newDuckObject.angle = 0
                        }
                    }
                }
                if (duckSpwn.levelState == 2) {
                    if (duckSpwnPoint.instVars.id == 1 || duckSpwnPoint.instVars.id == 2) {
                        /**@type  {ISpriteInstance}  */
                        this.newDuckObject = DuckClass.createInstance("duckObjec", duckSpwnPoint.x, duckSpwnPoint.y, true)
                        newDuckObject.instVars.id = duckSpwnPoint.instVars.id
                        // 調整duck方向
                        if (duckSpwnPoint.instVars.dir == 1) {
                            this.newDuckObject.angle = 0
                        }
                    }
                }
                if (duckSpwn.levelState == 3) {
                    if (duckSpwnPoint.instVars.id == 1 || duckSpwnPoint.instVars.id == 2 || duckSpwnPoint.instVars.id == 3) {
                        /**@type  {ISpriteInstance}  */
                        this.newDuckObject = DuckClass.createInstance("duckObjec", duckSpwnPoint.x, duckSpwnPoint.y, true)
                        newDuckObject.instVars.id = duckSpwnPoint.instVars.id
                        // 調整duck方向
                        if (duckSpwnPoint.instVars.dir == 1) {
                            this.newDuckObject.angle = 0
                        }
                    }
                }
                if (duckSpwn.levelState == 4) {
                    if (duckSpwnPoint.instVars.id == 1 || duckSpwnPoint.instVars.id == 2 || duckSpwnPoint.instVars.id == 3 || duckSpwnPoint.instVars.id == 4) {
                        /**@type  {ISpriteInstance}  */
                        this.newDuckObject = DuckClass.createInstance("duckObjec", duckSpwnPoint.x, duckSpwnPoint.y, true)
                        newDuckObject.instVars.id = duckSpwnPoint.instVars.id
                        // 調整duck方向
                        if (duckSpwnPoint.instVars.dir == 1) {
                            this.newDuckObject.angle = 0
                        }
                    }
                }
            }
        }, duckSpwn.changeSpeedTime)
    }
}